package com.heima.article.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.es.ApArticleSearch;
import com.heima.article.es.ApArticleSearchRepository;
import com.heima.article.service.IApArticleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr_Su
 * @version v1.0
 * @date 2022/7/10 15:12
 * @desciption
 */
@SpringBootTest
public class ApArticleESInit {

    @Autowired
    private IApArticleService apArticleService;

    @Autowired
    private ApArticleSearchRepository apArticleSearchRepository;

    @Test
    public void initES(){
        // 查询未下架,未删除的文章
        LambdaQueryWrapper<ApArticle> lqwApArticle = new LambdaQueryWrapper<>();
        lqwApArticle.eq(ApArticle::getIsDelete,false);
        lqwApArticle.eq(ApArticle::getIsDown,false);
        List<ApArticle> articleList = apArticleService.list(lqwApArticle);

        List<ApArticleSearch> articleSearchList = new ArrayList<>();
        for (ApArticle apArticle : articleList) {
            ApArticleSearch apArticleSearch = new ApArticleSearch();
            BeanUtils.copyProperties(apArticle,apArticleSearch);
            articleSearchList.add(apArticleSearch);
        }
        // 放到ES中
        apArticleSearchRepository.saveAll(articleSearchList);
    }
}
